package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServicrImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;


    @Autowired
    private DishFlavorMapper dishFlavorMapper;


    @Autowired
    private SetmealDishMapper setmealDishMapper;


    @Override
    @Transactional
    public void insert(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);

        dishMapper.insert(dish);

        //根据id关联口味进行添加
        Long id = dish.getId();

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(f -> {
                f.setDishId(id);
            });
        }
        dishFlavorMapper.insert(flavors);
    }


    //菜品分类
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());

        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);

        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());

        return pageResult;
    }


    //批量删除菜品
    @Override
    @Transactional
    public void deletedish(List<Long> ids) {
        //1.正在起售菜品不能删除
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            if (dish.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //2.被套餐关联的菜品不能删除
        List<Long> setmeal = setmealDishMapper.SetmealDishId(ids);
        if (setmeal != null && setmeal.size() > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        for (Long id : ids) {
            //3.删除菜品
            dishMapper.deleteDish(id);
            //4.口味删除
            dishFlavorMapper.deleteFlavor(id);
        }


    }


    @Override
    public DishVO selectById(Long id) {
        Dish dh = dishMapper.getById(id);

        List<DishFlavor> dishFlavorList = dishFlavorMapper.selectById(id);

        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dh, dishVO);
        dishVO.setFlavors(dishFlavorList);
        return dishVO;
    }


    //修改
    @Override
    @Transactional
    public void updateDish(DishDTO dishDTO) {
        Dish dish = new Dish();

        BeanUtils.copyProperties(dishDTO, dish);

        //修改菜品基本信息
        dishMapper.updateDish(dish);

        //删除原有的口味数据
        dishFlavorMapper.deleteFlavor(dishDTO.getId());

        //重新插入口味数据
        List<DishFlavor> dishFlavorList = dishDTO.getFlavors();

        if (dishFlavorList != null && dishFlavorList.size() > 0) {
            dishFlavorList.forEach(f -> {
                f.setDishId(dishDTO.getId());
            });

            dishFlavorMapper.insert(dishFlavorList);
        }

    }



    //根据分类id查询菜品
    @Override
    public List<Dish> list(Long categoryId) {
        List<Dish> dishes = setmealDishMapper.list(categoryId);
        return dishes;
    }



    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        Long categoryId = dish.getCategoryId();
        List<Dish> dishList = dishMapper.list(categoryId);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.selectById(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }



    @Override
    public void updataeStatus(Integer status, Long id) {
         dishMapper.updataeStatus(status,id);
    }
}
